<?php
/**
 * 创建 全品类热度飙升榜
 * 更新时间：每周一，3点41
 */
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\bigData\BrandFullCateRankingHot;
use Biz\model\wiki\WikiRankingCateHotUp;
use Biz\model\wiki\WikiRankingWeek;
use Biz\service\wikiRanking\Ranking;
use Biz\tools\Mail;
use Biz\service\wikiRanking\AdmRanking;


$start = time();
$dayArr = Ranking::getCreateRankingDay();
$day = $dayArr['day'];
$sourceDay = $dayArr['source_day'];
$now = date('Ymd');
if ($sourceDay != $now) {
    var_dump('当前日期为：' . $now . '不能创建：' . $sourceDay . '的榜单');
    return false;
}
$info = WikiRankingWeek::getByWhere(['ranking_day' => $day]);
if (!$info) {
    var_dump('不存在的榜单期数');
    return false;
}

$list = [];
$bigData = BrandFullCateRankingHot::select('*', ['day' => $sourceDay,
    'ORDER' => ['total_score' => 'DESC' ,'rate' => 'DESC'],
    'LIMIT' => [0,400]
]);
if (empty($bigData)) {
    var_dump('未查询到榜单数据');
    Mail::send(['tianhuining@haohaozhu.com', 'caowenxue@haohaozhu.com'], '未查询到榜单数据', '未查询到榜单数据#sql:' . json_encode(BrandFullCateRankingHot::log()));
    return false;
}
$hotUpRankingList = [];
$bigBrandCount = [];
 //一个品牌同一个榜单下只能有2个商品
foreach ($bigData as  $row) {
    if($bigBrandCount[$row['brand_id']] < 2 )  {
        $bigBrandCount[$row['brand_id']] += 1;
        $list[] = $row;
    }
}
$list = array_slice($list , 0,100);
//获取picId
$basic_pic_list = AdmRanking::getBasicPicIdsByRankingList($list);
//获取picId

foreach ($list as $row) {
    $data = [
        'ranking_day' => $day,
        'brand_id' => $row['brand_id'],
        'score' => $row['total_score'],
        'up_percent' => $row['rate'],
        'basic_id' => $row['basic_id'],
        'basic_pic_id' => $basic_pic_list[$row['basic_id']] ? : '',
        'addtime' => date('Y-m-d H:i:s'),
        'edittime' => date('Y-m-d H:i:s'),
    ];
    var_dump('创建 热度飙升榜单');
    WikiRankingCateHotUp::insert($data);
}
$end = time();
var_dump(date('Y-m-d-H-i-s') . '第' . $day . '期榜单,创建榜单-热度飙升榜,耗时:' . ($end - $start) . '秒，' . (($end - $start) / 60) . '分');

